jQuery(function($) {
	var $DEVELOPER_ID = "feaeec38b8bc37411b14de274b1d9480";
	var $AFFILIATE_ID = "0a30aaaf.afb9b1e2.0a30aab0.82880378";
	var $API_BASE_URL = "http://api.rakuten.co.jp/rws/3.0/json";
	var $ICON_IMAGE_URL = '[+icon+]';

	var Itens = [];
	var now_item = 0;
	var request_title;
	$.ajax({
	  url : $API_BASE_URL,
	  data : {
		  developerId : $DEVELOPER_ID,
		  affiliateId : $AFFILIATE_ID,
		  operation : 'BooksBookSearch',
		  version : '2011-12-01',
		  title : '[+keyword+]'
	  },
	  dataType : 'jsonp',
	  jsonp : 'callBack',
	  complete : function(json){
	  },
	  success : function(data, status){
		  console.log(JSON.stringify(data));
		  if (status == "success") {
			  $('#RakutenSearchWidget').hide().css({'backgroundImage':'url('+$ICON_IMAGE_URL+')','background-repeat': 'no-repeat'});;
			  if (data.Header != undefined && data.Header.Status.toLowerCase() == "success") {
				  request_title = data.Header.Args.Arg.title.value;
				  Itens = data.Body.BooksBookSearch.Items.Item;
				  show_item();
			  }
		  }
	  },
	  beforeSend : function(){
	  },
	  error : function(){
	  }
	});

	function show_item () {
		var Item = Itens[now_item];
		$('#RakutenSearchWidget').empty();
		var a_tag = $('<a></a>').attr({'href':Item.affiliateUrl,'title':Item.title,'target':'_blank'});
		var request_title_a = a_tag.clone();
		request_title_a.append(Item.title).css({'padding':'17px'});
		var request_title_div = $('<div></div>').append(request_title_a);
		var image_tag = $('<img />').attr({'src':Item.mediumImageUrl,'alt':Item.title});
		a_tag.append(image_tag);
		var main_div = $('<div></div>').append(request_title_div).append(a_tag);
		$('#RakutenSearchWidget').append(main_div).fadeIn().delay(6000).fadeOut(function (){
			if (now_item < Itens.length) {
				now_item++;
			} else {
				now_item = 0;
			}
			show_item();
		});
	}


});

